<template>
<div class="base-container">
  <t-row :gutter="14" style="height: 100%">
    <t-col :span="3" >
      <t-input
        style="margin-bottom: 10px"
        clearable
        placeholder="请输入单位名称"
        v-model="deptName"
        @change="deptNameTreeFilter"
      >
        <template #prefix-icon>
          <t-icon name="search" class="icon" size="16"/>
        </template>
      </t-input>
      <t-tree activable ref="tree"  @active="onActive" :data="deptOptions" :keys="{value:'id'}" @click="handleNodeClick" :expandAll="false" :filter="filterByText" hover :expand-on-click-node="false"  />
    </t-col>
    <t-col :span="9"  style="border-left: 1px solid #E5E6EB;height: 100%">
      <t-form ref="form" :data="queryParams" label-width="80" @reset="onReset" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">

        <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
          <t-col :span="11" class="query-form-inline">
            <t-form-item label="姓名" name="name">
              <t-input
                v-model="queryParams.name"
                clearable
                placeholder="请输入用户名称"
                style="width: 240px"
              />
            </t-form-item>
            <t-form-item label="手机号码" name="phonenumber">
              <t-input
                v-model="queryParams.phonenumber"
                clearable
                placeholder="请输入手机号码"
                style="width: 240px"
              />
            </t-form-item>
          </t-col>
          <t-col :span="1" class="operation-container">
            <t-button theme="primary" type="submit">查询</t-button>
            <t-button theme="default" variant="outline" type="reset">重置</t-button>
          </t-col>
        </t-row>
      </t-form>
      <div class="btn-group-inline">
        <t-button   @click="dialog.open = true;dialog.header='新增人员';dialog.objId=null" variant="outline" theme="primary">
          <t-icon name="add" slot="icon"/>
          新增
        </t-button>
      </div>
      <t-table
        rowKey="id"
        :data="data"
        :columns="queryParams.sysDeptName?columns:columns.filter((val)=>val.colKey!=='sort')"
        hover
        :pagination="pagination"
        :loading="dataLoading"
      >
        <template #duty="{ row }">
          <div v-if="['99999',''].includes(row.profTit)">
            {{ row.duty }}
          </div>
          <div v-else>
            {{ row.duty }}/{{ row.profTit | filterByDict(dict.type.prof_tit) }}
          </div>
        </template>
        <template #op="{ row }">
        <t-button @click="dialog.header='修改人员';dialog.objId=row.id;dialog.open = true;" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
</template>
      </t-table>
      <add-edit-team-member @reload="getList()" :departmentId.sync="queryParams.departmentId" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open" v-if="dialog.open"></add-edit-team-member>
    </t-col>
  </t-row>
</div>
</template>

<script>
import AddEditTeamMember from "./components/add-edit-team-member.vue";

export default {
  name: "member-index",
  dicts: ['prof_tit'],
  components:{
    AddEditTeamMember
  },
  data(){
    return {
      activeIds: [],
      dataLoading: false,
      filterByText: null,
      deptOptions: [],
      dialog: {
        header: '',
        open: false,
        objId: null,
      },
      deptName: '',
      data: [],
      columns: [
        {
          colKey: "sort",
          title: "序号",
          align: "center"
        },
        {
          colKey: "name",
          title: "姓名",
          align: "center"
        },
        {
          colKey: "duty",
          title: "职务/职称",
          align: "center"
        },
        {
          colKey: "sysDeptName",
          title: "单位名称",
          align: "center"
        },
        {
          colKey: "section",
          title: "所属部门",
          align: "center"
        },
        {
          colKey: "phonenumber",
          title: "手机号",
          align: "center"
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
          width: 180
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
        }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departmentId: null,
        name: '',
        phonenumber: '',
        sysDeptName: ''
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods:{
    getList(){
      this.dataLoading = true;
      this.$api.wlaq.emergency.teamList(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onReset(){
      this.queryParams.departmentId = null;
      this.queryParams.sysDeptName = '';
      this.deptNameTreeValue = [];
      this.pagination.current = 1;
      this.queryParams.pageNum = 1;
      // this.deptName = '';
      // 重置tree的高亮
      const { tree } = this.$refs;
      this.activeIds.forEach(val=>{
        tree.setItem(val, {
          actived: false,
        });
      })
      this.getList();
    },
    getTreeselect() {
      this.$api.system.dept.treeSelectAllIn().then((res) => {
        this.deptOptions = res.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    deptNameTreeFilter(){
      if (this.deptName) {
        // 存在过滤文案，才启用过滤
        this.filterByText = (node) => {
          const rs = node.data.label.indexOf(this.deptName) >= 0;
          // 命中的节点会强制展示
          // 命中节点的路径节点会锁定展示
          // 未命中的节点会隐藏
          return rs;
        };
      } else {
        // 过滤文案为空，则还原 tree 为无过滤状态
        this.filterByText = null;
      }
    },
    onActive(vals, state) {
      this.activeIds = vals;
    },
    handleNodeClick(content) {
      this.queryParams.departmentId = content.node.data.id;
      this.queryParams.sysDeptName = content.node.data.label;
      this.getList();
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除姓名为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          this.$api.wlaq.emergency.teamMemberDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
</style>
